<?php

/**
 * 分类管理
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\WholesalOrder as WholesalOrderService;
use app\admin\validate\WholesalOrder as WholesalOrderValidate;

class WholesalOrder extends BaseController
{
    /**
     * 获取分类列表
     * @param Request $request
     * @param WholesalOrderService $WholesalOrderService
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['order_no'] =  $request->param('order_no', null);
        $param['user_id'] =  $request->param('user_id', null);
        $param['status'] =  $request->param('status', null);
        $param['pay_status'] =  $request->param('pay_status', null);
        $param['exceptionstatus'] =  $request->param('exceptionstatus', null);
        $param['aftersales_status'] =  $request->param('aftersales_status', null);
        $param['start_time'] =  $request->param('start_time', null);
        $param['end_time'] =  $request->param('end_time', null);
        $param['username'] = $request->param('username', null);
        $param['product_id'] = $request->param('product_id', null);
        $param['warehouse_id'] = $request->param('warehouse_id', null);
        $param['is_futures'] = $request->param('is_futures', null);
        $param['id'] = $request->param('id', null);
        if ($param['user_id'] == null && !isSuperAdmin($request->operator_id)) { //普通用户
            $operatorService = new \app\admin\service\Operator();
            $userResult = $operatorService->getBelongCustomers($request->operator_id);
            if (0 == $userResult['code']) {
                if (1 == $userResult['data']['order_range']) {
                    $param['user_id'] =  $userResult['data']['customer_ids'];
                }
            } else {
                $param['user_id'] =  [0];
            }
        }
        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $WholesalOrderService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 新增经销商订单
     * @param Request $request
     * @param WholesalOrderService $WholesalOrderService
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {

        $checkData = $request->param();
        if (!$validate->scene('save')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['operator_id'] = $request->operator_id;

        $param['user_id'] =  $request->param('user_id', null);
        $param['order_no'] =  $request->param('order_no', null);
        $param['notes'] = $request->param('notes', null);
        $param['order_remark'] = $request->param('order_remark', null);
        $param['product_list'] =  $request->param('product_list', []);
        $param['order_remark'] =  $request->param('order_remark', null);
        $param['first_leg_logistics_fee'] = $request->param('first_leg_logistics_fee', 0.0);
        $param['currency'] = $request->has('currency') ? intval($request->param('currency')) : null;
        $param['signatory'] = $request->has('signatory') ? trim($request->param('signatory')) : null;
        $param['signing_date'] = $request->has('signing_date') ? trim($request->param('signing_date')) : null;
        $param['owner'] = $request->has('owner') ? intval($request->param('owner')) : null;
        $param['partner'] = $request->has('partner') ? trim($request->param('partner')) : null;
        $param['single_order_method'] = $request->has('single_order_method') ? intval($request->param('single_order_method')) : null;
        $param['id_number'] = $request->has('id_number') ? trim($request->param('id_number')) : null;
        $param['payment_deadline'] = $request->has('payment_deadline') ? trim($request->param('payment_deadline')) : null;
        $param['shipping_start_date'] = $request->has('shipping_start_date') ? trim($request->param('shipping_start_date')) : null;
        $param['shipping_end_date'] = $request->has('shipping_end_date') ? trim($request->param('shipping_end_date')) : null;
        $param['signing_date'] = $request->has('signing_date') ? trim($request->param('signing_date')) : null;
        $param['transfer_payment_info'] = $request->has('transfer_payment_info') ? trim($request->param('transfer_payment_info')) : null;
        $param['contract_type'] = $request->has('contract_type') ? trim($request->param('contract_type')) : null;
        $param['pay_terms'] = $request->has('pay_terms') ? intval($request->param('pay_terms')) : null;
        $param['advance_amount'] = $request->has('advance_amount') ? floatval($request->param('advance_amount')) : null;
        $param['source'] = $request->has('source') ? intval($request->param('source')) : 0;
        $param['is_sop'] = $request->has('is_sop') ? intval($request->param('is_sop')) : 0; // 是否拼单采购(Single Order Procurement) 0 - 否 1 - 是


        $arrData = $WholesalOrderService->save($param);
        return json($arrData);
    }

    /**
     * 修改经销商订单
     * @param Request $request
     * @param WholesalOrderService $WholesalOrderService
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-12 16 53 52
     * author: jiang
     */
    public function update(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('update')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;

        $param['id'] =  $request->param('id', null);
        $param['user_id'] =  $request->param('user_id', null);
        $param['product_list'] =  $request->param('product_list', []);
        $param['order_remark'] =  $request->param('order_remark', null);
        $param['notes'] = $request->param('notes', null);
        $param['order_remark'] = $request->param('order_remark', null);
        $param['first_leg_logistics_fee'] = $request->param('first_leg_logistics_fee', 0.0);
        $param['order_no'] =  $request->param('order_no', null);
        $param['currency'] = $request->has('currency') ? intval($request->param('currency')) : null;
        $param['signatory'] = $request->has('signatory') ? trim($request->param('signatory')) : null;
        $param['signing_date'] = $request->has('signing_date') ? trim($request->param('signing_date')) : null;
        $param['owner'] = $request->has('owner') ? intval($request->param('owner')) : null;
        $param['partner'] = $request->has('partner') ? trim($request->param('partner')) : null;
        $param['single_order_method'] = $request->has('single_order_method') ? intval($request->param('single_order_method')) : null;
        $param['id_number'] = $request->has('id_number') ? trim($request->param('id_number')) : null;
        $param['payment_deadline'] = $request->has('payment_deadline') ? trim($request->param('payment_deadline')) : null;
        $param['shipping_start_date'] = $request->has('shipping_start_date') ? trim($request->param('shipping_start_date')) : null;
        $param['shipping_end_date'] = $request->has('shipping_end_date') ? trim($request->param('shipping_end_date')) : null;
        $param['signing_date'] = $request->has('signing_date') ? trim($request->param('signing_date')) : null;
        $param['transfer_payment_info'] = $request->has('transfer_payment_info') ? trim($request->param('transfer_payment_info')) : null;
        $param['contract_type'] = $request->has('contract_type') ? trim($request->param('contract_type')) : null;
        $param['pay_terms'] = $request->has('pay_terms') ? intval($request->param('pay_terms')) : null;
        $param['advance_amount'] = $request->has('advance_amount') ? floatval($request->param('advance_amount')) : null;
        $param['source'] = $request->has('source') ? intval($request->param('source')) : 0;
        $param['is_sop'] = $request->has('is_sop') ? intval($request->param('is_sop')) : 0; // 是否拼单采购(Single Order Procurement) 0 - 否 1 - 是

        $arrData = $WholesalOrderService->update($param);
        return json($arrData);
    }

    /**
     * 分类详情
     * @param Request $request 分类ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $arrData = $WholesalOrderService->getInfo($param);
        return json($arrData);
    }
    /**
     * 订单复制
     * @param Request $request 分类ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function copy(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $arrData = $WholesalOrderService->copy($param);
        return json($arrData);
    }

    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param WholesalOrderValidate $validate
     * @param WholesalOrderService $WholesalOrderService
     * @return \think\response\Json
     */
    public function delete(Request $request, WholesalOrderValidate $validate, WholesalOrderService $WholesalOrderService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $WholesalOrderService->delete($param);
        return json($arrData);
    }

    /**
     * 下拉框获取分类信息
     * @param Request $request
     * @param WholesalOrderService $WholesalOrderService
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $arrData = $WholesalOrderService->getSelect();
        return json($arrData);
    }

    /**
     * 批量发货
     * @param Request $request
     * @param WholesalOrderService $WholesalOrderService
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     * 2024-11-17 14 11 59
     * author: jiang
     */
    public function deliver(Request $request, WholesalOrderService $WholesalOrderService, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();

        $param['delivery_method'] = $request->param('delivery_method', null);

        $scene = $param['delivery_method'] == 1 ? 'deliver_express' : 'deliver_self';
        if (!$validate->scene($scene)->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['name'] = $request->param('name', null);
        $param['phone'] = $request->param('phone', null);
        $param['passport'] = $request->param('passport', null);
        $param['freight_collect'] = $request->param('freight_collect', null);
        $param['vehicle'] = $request->param('vehicle', null);
        $param['warehouse_code'] = $request->param('warehouse_code', null);
        $param['address_id'] = $request->param('address_id', null);
        $param['billing_address_id'] = $request->param('billing_address_id', null);
        $param['prescription'] = $request->param('prescription', null);
        $param['tax_package_id'] = $request->param('tax_package_id', null);
        $param['user_id'] = $request->param('user_id', null);
        $param['wid'] = $request->param('wid', null);
        $param['list'] = $request->param('list', null);
        $param['owner'] = $request->param('owner', null);
        $param['partner'] = $request->param('partner', null);
        $param['logistics_fee'] = $request->param('logistics_fee', 0.0);
        $param['notes'] = $request->param('notes', null);
        $param['order_remark'] = $request->param('order_remark', null);

        $arrData = $WholesalOrderService->deliver($param);
        return json($arrData);
    }

    /**
     * 订单审核
     * @param Request $request
     * @param WholesalOrderService $service
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-22 15 46 37
     * author: jiang
     */
    public function examine(Request $request, WholesalOrderService $service, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('examine')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $service->examine($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request, WholesalOrderService $WholesalOrderService)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $WholesalOrderService->getFieldEnum($param);
        return json($arrData);
    }

    /**
     * 取消订单
     * @param Request $request
     * @param WholesalOrderService $service
     * @param WholesalOrderValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2025-03-11 18 02 33
     * author: jiang
     */
    public function cancel(Request $request, WholesalOrderService $service, WholesalOrderValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('cancel')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $service->cancel($param);
        return json($arrData);
    }
}
